package com.paradise.phone;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

import com.paradise.phone.aidl.IKeepLiveInterface;

public class FacephoneService extends Service {

    private static final int INCOMMING_CALL = 0x001;
    private FacePhoneBinder fpBinder;
    private FacephoneConnection fphoneConnection;

    public FacephoneService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return fpBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        fpBinder = new FacePhoneBinder();
        fphoneConnection = new FacephoneConnection();

        RemoteViews remoteView = new RemoteViews("com.paradise.facephone", R.layout.incomming_call_remote_view);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.mipmap.facephone)
                .setContent(remoteView)
                .setDefaults(1)
                .build();

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0x001, notification);

        Log.d("flag->", "FacephoneService.java(onCreate:40)->"+"服务开启");

        /*FacePhone.instance().getmLc().addListener(new LinphoneCoreListenerBase() {
            @Override
            public void callState(LinphoneCore linphoneCore, LinphoneCall linphoneCall, LinphoneCall.State state, String s) {

                if (true) return;

                if (state.equals(LinphoneCall.State.IncomingReceived)){
                    Log.d("flag->", "FacephoneService.java(callState:47)->"+"收到电话");
                    if (FacephonePreferences.instance().isAutoStartEnabled()){
                        FaceCall.instance().acceptCall();
                        Intent intent = new Intent(getApplicationContext(), TalkingAcitvity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                        if (linphoneCall.getRemoteParams().getVideoEnabled())
                            intent.putExtra("INCOMMING_CALL_TYPE", "video");
                        else
                            intent.putExtra("INCOMMING_CALL_TYPE", "audio");
                        startActivity(intent);
                    }else {
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        intent.putExtra("INCOMMING_CALL", "incomming_call");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                        startActivity(intent);
                    }
                }
            }
        });*/
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        this.bindService(new Intent(this, KeepLiveService.class),
                fphoneConnection, BIND_IMPORTANT);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("flag->", "FacephoneService.java(onDestroy:58)->"+"服务销毁");
    }

    public class FacePhoneBinder extends IKeepLiveInterface.Stub {

        @Override
        public String getServiceName() throws RemoteException {
            return "FacephoneService";
        }
    }

    public class FacephoneConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("flag->", "FacephoneService.java(onServiceConnected:101)->"+"守护进程连接成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("flag->", "FacephoneService.java(onServiceDisconnected:106)->"+"守护进程被杀死");
            FacephoneService.this.startService(new Intent(FacephoneService.this, KeepLiveService.class));
            FacephoneService.this.bindService(new Intent(FacephoneService.this, KeepLiveService.class),
                    fphoneConnection, BIND_IMPORTANT);
        }
    }

}
